package tc.de.krawallmann.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * The Class MD5 calculates an MD5 checksum for a string.
 * @author KrawallMann
 * @version 1.0
 */
public final class MD5 {

	/**
	 * Calculates the MD5 checksum.
	 * 
	 * @param string
	 *            the String to be used
	 * @return the MD5 checksum of the given String
	 */
	public static String md5(final String string) {
		MessageDigest md5;
		String ret;
		try {
			md5 = MessageDigest.getInstance("MD5");

			md5.reset();
			md5.update(string.getBytes());
			final byte[] result = md5.digest();

			final StringBuilder hexString = new StringBuilder();
			for (final byte element : result) {
				hexString.append(Integer.toHexString(0xFF & element));
			}

			md5.reset();
			ret = hexString.toString();
		} catch (final NoSuchAlgorithmException ex) {
			ret = "FAILURE";
		}
		return ret;
	}

	/**
	 * Instantiates a new m d5.
	 */
	private MD5() {
		super();
		// Singleton constructor
	}
}
